<?php

class ProductController extends Zend_Controller_Action
{
    private $_params;
	private $_auth;	
    private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_auth        = new My_Authuser();
        $this->view->auth   = $this->_auth;        
        $this->_auth        = $this->_auth->checkAuthentication();
        $this->view->auth   = $this->_auth;
        if ($this->_helper->FlashMessenger->hasMessages()) {
            $this->view->messages = $this->_helper->FlashMessenger->getMessages();
        }                
    }
    public function indexAction(){        
        
    }
    public function reviveAction(){
        if($this->getRequest()->isPost()){
            $direc = $this->getRequest()->getPost('direc');
            if($direc=='submit'){
                $id = $this->getRequest()->getPost('id');                
                $endDate = $this->getRequest()->getPost('endDateCmt');            
                $configPrice = new Zend_Config_Ini(CONFIG_PATH.'/price.ini', APPLICATION_ENV);
                $price=$configPrice->price->toArray();                  
                $start = new DateTime();            
                $end = new DateTime($endDate);            
                $updateData['priceAd'] = ((int)$end->diff($start)->days+1)*$price['price'];
                $updateData['endDate'] = strtotime($endDate);
                $updateData['startDate'] = time();
                $updateData['status'] = 2;
                $modelP = new Default_Model_Product();                
                $modelP->updateWait($id,$updateData);
                $modelU = new Default_Model_User();
                $product = $modelP->getDataById($id);
                $user = $modelU->getDataUserById($product['userId']);
                $gold = $user['gold']-$updateData['priceAd'];                 
                $modelU->updateGold($user['id'],array('gold'=>$gold));                 
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'user','action'=>'proman'), 'user.proman', true);
            }elseif($direc=='cancel'){
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'user','action'=>'proman'), 'user.proman', true);                
            }                                                     
        }else{
            $id = $this->_getParam('id', 0); 
            if(!$id){
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'user','action'=>'proman'), 'user.proman', true);
            }                       
            $modelP = new Default_Model_Product();
            $modelU = new Default_Model_User();
            $productCheck = $modelP->getDataById($id);
            if(isset($productCheck)){
                $configCar= new Zend_Config_Ini(CONFIG_PATH.'/human.ini', APPLICATION_ENV);
                $huL=$configCar->human->toArray();
                $configCar= new Zend_Config_Ini(CONFIG_PATH.'/transaction.ini', APPLICATION_ENV);
                $trL=$configCar->transaction->toArray();
                $productCheck['human'] = $huL[$productCheck['human']];
                $productCheck['transaction'] = $trL[$productCheck['transaction']];
                $userCheck = $modelU->getDataUserById($productCheck['userId']);                
                $this->view->userCheck = $userCheck;
                $this->view->proCheck = $productCheck;
                $this->view->navi = 'Làm mới tin';
            }else{
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'user','action'=>'proman'), 'user.proman', true);
            }                        
        }            
    }
    public function topAction(){
        $modelP= new Default_Model_Product();
        $modelU = new Default_Model_User();
        $configPrice= new Zend_Config_Ini(CONFIG_PATH.'/price.ini', APPLICATION_ENV);
        $dataPro= $modelP->getDataById($this->_params['id']);
        $userCheck = $modelU->getDataUserById($dataPro['userId']); 
        if($dataPro['userId']!=$this->_auth['userId']){
            $this->view->mess = "Có lỗi xảy ra";
            $this->view->flag = "error";  
        }
        if($dataPro['type']=='vi' && $dataPro['status']==2 && time()<$dataPro['endDate']){
            $configCar= new Zend_Config_Ini(CONFIG_PATH.'/human.ini', APPLICATION_ENV);
            $huL=$configCar->human->toArray();
            $configCar= new Zend_Config_Ini(CONFIG_PATH.'/transaction.ini', APPLICATION_ENV);
            $trL=$configCar->transaction->toArray();
            
            $this->view->priceTop=$configPrice->price->refresh->top;
            $dataPro['human'] = $huL[$dataPro['human']];
            $dataPro['transaction'] = $trL[$dataPro['transaction']];
                        
            $this->view->userCheck = $userCheck;
            $this->view->proCheck = $dataPro;
            $this->view->navi = 'Đưa tin lên top';                                   
        }else{
            $this->view->mess = "Tin này không hợp lệ";
            $this->view->flag = "alert";
        }
        if($this->getRequest()->isPost()){ 
            $formData = $this->getRequest()->getPost();
            if($formData['direc']=='submit'){
                $goldUpdate=$userCheck['gold']-($configPrice->price->refresh->top);
                if(isset($goldUpdate)){
                    $modelU->updateGold($dataPro['userId'],array('gold'=>$goldUpdate));
                    $modelP->updateAll($this->_params['id'],array('startDate'=>time()));                    
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'index','action'=>'index'), 'index.index', true);
                }
            }else{
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'user','action'=>'proman'), 'user.proman', true);
            }
        }
    }
    public function reviewAction(){
        if($this->getRequest()->isPost()){            
            $direc = $this->getRequest()->getPost('direc');
            if($direc=='submit'){
                $id = $this->getRequest()->getPost('id');                
                $type = $this->getRequest()->getPost('type');
                $modelP = new Default_Model_Product();                
                $modelP->updateWait($id,array('status'=>'1'));
                $proCheck = $modelP->getDataById($id);
                if($type=='no'){
                    $this->view->navi='Tin đăng thường (miễn phí)';
                }else{                    
                    $modelU = new Default_Model_User();
                    $userCheck = $modelU->getDataUserById($this->_auth['userId'])->toArray();
                    $goldLeft = (int)$userCheck['gold']-(int)$proCheck['priceAd'];                                        
                    $modelU->updateGold($this->_auth['userId'],array('gold'=>$goldLeft));
                    $this->view->navi='Tin đăng VIP';
                }                                
                $this->_helper->flashMessenger->addMessage(array('success'=>'Tin đang chờ duyệt (trong vòng từ 6 đến 12 tiếng).<br>Theo dõi tiến độ duyệt tin ở mục Quản lý tin.'));                                
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'result'), 'product.result', true);                                                        
            }elseif($direc=='cancel'){
                $id = $this->getRequest()->getPost('id');                
                $modelP = new Default_Model_Product();
                $proCheck = $modelP->getDataById($id);
                if(isset($proCheck)){
                    $proCheck = $proCheck->toArray();
                    foreach(json_decode($proCheck['photo']) as $o){
                        unlink(WEB_PATH.$o);   
                    }                                
                    $modelP->deleteData($id);                                    
                    $this->_helper->flashMessenger->addMessage(array('alert'=>'Tin đã huỷ'));                                
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'result'), 'product.result', true);
                }else{
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'index','action'=>'index'), 'index.index', true);
                }                                                                
            }elseif($direc=='edit'){                
                $id = $this->getRequest()->getPost('id');
                $type = $this->getRequest()->getPost('type');
                if($type=='no'){                    
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'post','id'=>$id,'title'=>'dang-tin-thuong'),'product.editpost',true);    
                }else{                    
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'postvip','id'=>$id,'title'=>'dang-tin-vip'),'product.editpostvip',true);
                }                                
            }
        }else{
            $id = $this->_getParam('id', 0);
            $modelP = new Default_Model_Product();
            $proCheck = $modelP->getDataById($id);            
            if($proCheck){
                $proCheck = $proCheck->toArray();                
                if($proCheck['status']==0 && $proCheck['userId']==$this->_auth['userId']){
                    $configCar= new Zend_Config_Ini(CONFIG_PATH.'/human.ini', APPLICATION_ENV);
                    $huL=$configCar->human->toArray();
                    $configCar= new Zend_Config_Ini(CONFIG_PATH.'/transaction.ini', APPLICATION_ENV);
                    $trL=$configCar->transaction->toArray();
                    $proCheck['human'] = $huL[$proCheck['human']];
                    $proCheck['transaction'] = $trL[$proCheck['transaction']];                            
                    $this->view->proCheck = $proCheck;
                    if($proCheck['transaction']=='no'){
                        $this->view->navi = 'Tin đăng thường (miễn phí)';    
                    }else{
                        $this->view->navi = 'Tin đăng VIP';
                    }
                }else{                
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'index','action'=>'index'), 'index.index', true);
                }                                                         
            }else{                
                $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'index','action'=>'index'), 'index.index', true); 
            }       
        }                         
    }    
    public function postAction(){          
        if(!isset($this->_auth)){
            $this->_helper->redirector('login','auth','default');     
        }
        $form = new Default_Form_Product();        
        $form->removeElement('startDate');
        $form->removeElement('endDate');
        $form->removeElement('priceAd');
        $modelUser = new Default_Model_User();
        $authInfo = $modelUser->getDataUserById($this->_auth['userId'])->toArray();  
        $form->contact->setValue($authInfo['fullname']);
        $form->mobile->setValue($authInfo['mobile']);
        $form->emailCont->setValue($authInfo['email']);
        $form->addressCont->setValue($authInfo['address']);
        $this->view->postAdForm = $form;        
        if($this->getRequest()->isPost()){                
            if($this->getRequest()->getPost('id')==0){                
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){                   
                    $addData['title']       = $form->getValue('title');                
                    $addData['shortTitle']  = $this->_helper->setup($form->getValue('title'));
                    $addData['createDate']  = time();                                                                                
                    $addData['content']     = $form->getValue('content');
                    $addData['price']       = $form->getValue('price');
                    $addData['human']       = $form->getValue('human');
                    $addData['itemId']      = $form->getValue('itemId');
                    $addData['areaId']      = $form->getValue('areaId');
                    $addData['transaction'] = $form->getValue('transaction');                                                                            
                    $addData['type']        = $form->getValue('type');
                    $addData['contact']     = $form->getValue('contact');
                    $addData['mobile']      = $form->getValue('mobile');
                    $addData['emailCont']   = $form->getValue('emailCont');
                    $addData['addressCont'] = $form->getValue('addressCont');
                    $addData['type']        = 'no';
                    $addData['status']      = 0;
                    $addData['userId']      = $this->_auth['userId'];
                    
                    $modelProduct = new Default_Model_Product();                                    
                    $upload = new Zend_File_Transfer();                
                    $files = $upload->getFileInfo();
                    $i=1;                                                                                
                    foreach($files as $f){
                        $upload = new Zend_File_Transfer(); 
                        $upload->setDestination(PUBLIC_PATH.'/upload/product/');
                        if($f['name']){                        
                            $fileName = time().'_'.$i++.'_'.$f['name'];                            
                            $upload->addFilter('Rename',$fileName);
                            $upload->receive($f['name']);
                            $addData['photo'][] = '/public/upload/product/'.$fileName;                           
                        }                    
                    }                                                                                                                                                                                                                                                                                             
                    $addData['photo'] = json_encode($addData['photo']);
                    
                    $addId = $modelProduct->addData($addData);                
                    if($addId){
                        $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'review','id'=>$addId,'title'=>$addData['shortTitle']), 'product.review', true);                        
                    }else{
                        $this->view->mess ="Đã có lỗi xảy ra";
                        $this->view->flag = "alert";
                    }                				
                }else{                    
                    $form->populate($formData);
                }    
            }else{
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){                   
                    $updateData['id']           = $form->getValue('id');                    
                    $updateData['title']        = $form->getValue('title');
                    $updateData['shortTitle']   = $this->_helper->setup($form->getValue('title'));
                    $updateData['createDate']   = time();                
                    $updateData['shortTitle']   = $this->_helper->setup($form->getValue('title'));                                                                                                    
                    $updateData['content']      = $form->getValue('content');
                    $updateData['price']        = $form->getValue('price');
                    $updateData['human']        = $form->getValue('human');
                    $updateData['itemId']       = $form->getValue('itemId');
                    $updateData['areaId']       = $form->getValue('areaId');
                    $updateData['transaction']  = $form->getValue('transaction');                                                                            
                    $updateData['type']         = $form->getValue('type');
                    $updateData['contact']      = $form->getValue('contact');
                    $updateData['mobile']       = $form->getValue('mobile');
                    $updateData['emailCont']    = $form->getValue('emailCont');
                    $updateData['addressCont']  = $form->getValue('addressCont');
                    $updateData['type']         = 'no';
                    $updateData['status']       = 0;
                    $updateData['userId']       = $this->_auth['userId'];
                    
                    $modelProduct = new Default_Model_Product();
                    $proCheck =$modelProduct->getDataById($updateData['id'])->toArray();                                     
                    $upload = new Zend_File_Transfer();                
                    $files = $upload->getFileInfo();
                    $i=1;                                                                                
                    foreach($files as $f){
                        $upload = new Zend_File_Transfer(); 
                        $upload->setDestination(PUBLIC_PATH.'/upload/product/');
                        if($f['name']){                        
                            $fileName = time().'_'.$i++.'_'.$f['name'];                            
                            $upload->addFilter('Rename',$fileName);
                            $upload->receive($f['name']);
                            $updateData['photo'][] = '/public/upload/product/'.$fileName;                           
                        }                    
                    }                 
                    if(!isset($updateData['photo']))
                        $updateData['photo']=$proCheck['photo'];   
                    else{
                        foreach(json_decode($proCheck['photo']) as $o){
                            unlink(WEB_PATH.$o);
                        }
                        $updateData['photo']=json_encode($updateData['photo']);
                    }                                                                                                                                                                                                                                                                                                                                                                                                    
                    $modelProduct->updateData($updateData['id'],$updateData);                                                        
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'review','id'=>$updateData['id'],'title'=>$updateData['shortTitle']), 'product.review', true);                                        
                } 
            }                                               
        }else{     
            $id = $this->_getParam('id', 0);
            if($id!=0){
                $modelP = new Default_Model_Product();
                $proCheck = $modelP->getDataById($id)->toArray();                
                if($proCheck){
                    $this->view->proCheck = $proCheck;                    
                    $form->populate($proCheck);                        
                }else{
                    $this->_helper->redirector('index','index','default');
                     
                }                   
            }            
        }        
    }
    public function postvipAction(){
        $form = new Default_Form_Product();
        $modelUser = new Default_Model_User();
        $authInfo = $modelUser->getDataUserById($this->_auth['userId'])->toArray();  
        $form->contact->setValue($authInfo['fullname']);
        $form->mobile->setValue($authInfo['mobile']);
        $form->emailCont->setValue($authInfo['email']);
        $form->addressCont->setValue($authInfo['address']);        
        $this->view->postAdForm = $form;        
        if($this->getRequest()->isPost()){  
            if($this->getRequest()->getPost('id')==0){                
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){
                    $configPrice = new Zend_Config_Ini(CONFIG_PATH.'/price.ini', APPLICATION_ENV);
                    $price=$configPrice->price->toArray();                  
                    $start = new DateTime($form->getValue('startDate'));
                    $start->setTime(date("H"),date("i"));
                    $end = new DateTime($form->getValue('endDate'));
                    $end->setTime(date("H"),date("i"));                                                                                                                
                    $addData['priceAd'] = ((int)$end->diff($start)->days+1)*$price['price'];
                    if($addData['priceAd']>$authInfo['gold']){
                        $this->view->mess ="Số tiền trong tài khoản không đủ thanh toán tin này !!!";
                        $this->view->flag = "alert";
                        $form->populate($formData);
                        return;
                    }                                                                
                    $addData['title']       = $form->getValue('title');                
                    $addData['shortTitle']  = $this->_helper->setup($form->getValue('title'));
                    $addData['createDate']  = time();
                    $addData['startDate']   = strtotime($start->format('Y-m-d H:i:s'));
                    $addData['endDate']     = strtotime($end->format('Y-m-d H:i:s'));                                                                                                    
                    $addData['content']     = $form->getValue('content');
                    $addData['price']       = $form->getValue('price');
                    $addData['human']       = $form->getValue('human');
                    $addData['itemId']      = $form->getValue('itemId');
                    $addData['areaId']      = $form->getValue('areaId');
                    $addData['transaction'] = $form->getValue('transaction');                                                                            
                    $addData['type']        = $form->getValue('type');
                    $addData['contact']     = $form->getValue('contact');
                    $addData['mobile']      = $form->getValue('mobile');
                    $addData['emailCont']   = $form->getValue('emailCont');
                    $addData['addressCont'] = $form->getValue('addressCont');
                    $addData['type']        = 'vi';
                    $addData['status']      = 0;
                    $addData['userId']      = $this->_auth['userId'];

                    $modelProduct = new Default_Model_Product();                                    
                    $upload = new Zend_File_Transfer();                
                    $files = $upload->getFileInfo();
                    $i=1;                                                                                
                    foreach($files as $f){
                        $upload = new Zend_File_Transfer(); 
                        $upload->setDestination(PUBLIC_PATH.'/upload/product/');
                        if($f['name']){                        
                            $fileName = time().'_'.$i++.'_'.$f['name'];                            
                            $upload->addFilter('Rename',$fileName);
                            $upload->receive($f['name']);
                            $addData['photo'][] = '/public/upload/product/'.$fileName;                           
                        }                    
                    }                                                                                                                                                                                                                                                                                             
                    $addData['photo'] = json_encode($addData['photo']);
                    
                    $addId = $modelProduct->addData($addData);                
                    if($addId){                        
                        $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'review','id'=>$addId,'title'=>$addData['shortTitle']), 'product.review', true);
                    }else{
                        $this->view->mess ="Đã có lỗi xảy ra";
                        $this->view->flag = "alert";
                    }
                }else{
                    $form->populate($formData);
                }
            }else{
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){
                    $configPrice = new Zend_Config_Ini(CONFIG_PATH.'/price.ini', APPLICATION_ENV);
                    $price=$configPrice->price->toArray();                                      
                    $start = new DateTime($form->getValue('startDate'));
                    $start->setTime(date("H"),date("i"));
                    $end = new DateTime($form->getValue('endDate'));
                    $end->setTime(date("H"),date("i"));                                                                           
                    $updateData['priceAd'] = ((int)$end->diff($start)->days+1)*$price['price'];                                    
                    if($updateData['priceAd']>$authInfo['gold']){
                        $this->view->mess ="Số tiền trong tài khoản không đủ thanh toán tin này !!!";
                        $this->view->flag = "alert";
                        $form->populate($formData);
                        return;
                    }                                        
                    $updateData['id']           = $form->getValue('id');                    
                    $updateData['title']        = $form->getValue('title');
                    $updateData['shortTitle']   = $this->_helper->setup($form->getValue('title'));
                    $updateData['createDate']   = time();
                    $updateData['startDate']    = strtotime($start->format('Y-m-d H:i:s'));
                    $updateData['endDate']      = strtotime($end->format('Y-m-d H:i:s'));                      
                    $updateData['shortTitle']   = $this->_helper->setup($form->getValue('title'));                                                                                                    
                    $updateData['content']      = $form->getValue('content');
                    $updateData['price']        = $form->getValue('price');
                    $updateData['human']        = $form->getValue('human');
                    $updateData['itemId']       = $form->getValue('itemId');
                    $updateData['areaId']       = $form->getValue('areaId');
                    $updateData['transaction']  = $form->getValue('transaction');                                                                            
                    $updateData['type']         = $form->getValue('type');
                    $updateData['contact']      = $form->getValue('contact');
                    $updateData['mobile']       = $form->getValue('mobile');
                    $updateData['emailCont']    = $form->getValue('emailCont');
                    $updateData['addressCont']  = $form->getValue('addressCont');
                    $updateData['type']         = 'vi';
                    $updateData['status']       = 0;
                    $updateData['userId']       = $this->_auth['userId'];
                    
                    $modelProduct = new Default_Model_Product();
                    $proCheck =$modelProduct->getDataById($updateData['id'])->toArray();                                     
                    $upload = new Zend_File_Transfer();                
                    $files = $upload->getFileInfo();
                    $i=1;                                                                                
                    foreach($files as $f){
                        $upload = new Zend_File_Transfer(); 
                        $upload->setDestination(PUBLIC_PATH.'/upload/product/');
                        if($f['name']){                        
                            $fileName = time().'_'.$i++.'_'.$f['name'];                            
                            $upload->addFilter('Rename',$fileName);
                            $upload->receive($f['name']);
                            $updateData['photo'][] = '/public/upload/product/'.$fileName;                           
                        }                    
                    }                 
                    if(!isset($updateData['photo']))
                        $updateData['photo']=$proCheck['photo'];   
                    else{
                        foreach(json_decode($proCheck['photo']) as $o){
                            unlink(WEB_PATH.$o);
                        }
                        $updateData['photo']=json_encode($updateData['photo']);
                    }                                                                                                                                                                                                                                                                                                                                                                                                    
                    $modelProduct->updateData($updateData['id'],$updateData);
                    $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'product','action'=>'review','id'=>$updateData['id'],'title'=>$updateData['shortTitle']), 'product.review', true);                                                                            
                }
            }                         
        }else{
            $id = $this->_getParam('id', 0);
            if($id!=0){
                $modelP = new Default_Model_Product();
                $proCheck = $modelP->getDataById($id)->toArray();                
                if($proCheck){
                    $this->view->proCheck = $proCheck;
                    $form->populate($proCheck);                        
                }else{
                    $this->_helper->redirector('index','index','default'); 
                }                   
            } 
        }         
    }
    public function resultAction(){        
        if(!$this->_helper->FlashMessenger->hasMessages()) {
            $this->_helper->redirector->setGotoRoute(array('module'=>'default','controller'=>'index','action'=>'index'), 'index.index', true);
        }        
    }
    public function detailAction(){
        $id = $this->_getParam('id', 0);                
        $modelP = new Default_Model_Product();
        $productCheck = $modelP->getDataById($id);
        if($productCheck){
            $productCheck = $productCheck->toArray();            
            if($productCheck['status']==2 && time()>$productCheck['startDate'] && time()<$productCheck['endDate']){                
                $productCheck['navi'] = $productCheck['areaName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$productCheck['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$productCheck['itemName'];  
                $this->view->productCheck = $productCheck;        
            }else{
                $this->_helper->redirector('index','index','default');
            }            
        }        
        
                        
    }
    public function searchAction(){
        if ($this->getRequest()->isPost()){            
            $formData = $this->getRequest()->getPost();            
            if(isset($formData['value']) && $formData['value']!='') 
                $searchData['title']=$formData['value'];
            if(isset($formData['catalog']) && $formData['catalog']!=-1) 
                $searchData['itemId']=$formData['catalog'];                            
            if(isset($formData['area']) && $formData['area']!=-1) 
                $searchData['areaId']=$formData['area'];
            if(isset($formData['transaction']) && $formData['transaction']!='') 
                $searchData['transaction']=$formData['transaction'];
            $searchData['wait']=2;
                                                
            $modelP = new Default_Model_Product();                       
            $searchData['type']='vi';
            $productVi = $modelP->getAllData($searchData)->toArray();
            $searchData['type']='no';
            $productNo = $modelP->getAllData($searchData)->toArray();
            foreach($productNo as $pro){
                array_push($productVi,$pro);   
            }                            
            $productA = $productVi;
            $productP = array();
            $productC = array();
            if(count($productA)>0){
                foreach($productVi as $pro){
                    ($pro['human']=='pe')?$productP[]=$pro:$productC[]=$pro;
                }
            }                                    
            $form = new Default_Form_Search();
            $this->view->form=$form;
            $this->view->productA = $productA;
            $this->view->productP = $productP;
            $this->view->productC = $productC;
                
            $modelItem = new Default_Model_Productitem();
            $modelArea = new Default_Model_Area();                       
            if(isset($searchData['areaId'])){                
                $area = $modelArea->getDataById($searchData['areaId'])->toArray();
                if(isset($searchData['itemId'])){            
                    $item = $modelItem->getDataById($searchData['itemId'])->toArray();
                    $this->view->navi = $area['name'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['name'];
                }else{
                    $this->view->navi = $area['name'];
                }                
            }elseif(isset($searchData['itemId'])){
                $item = $modelItem->getDataById($searchData['itemId'])->toArray();
                $this->view->navi = $item['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['name'];
            }elseif(isset($searchData['title'])){
                $this->view->navi = 'Tất cả chuyên mục';
            }else{
                $this->view->navi = 'Tất cả chuyên mục';
            }            
            $form->populate($formData);                                                                                                                                                      
            $this->_forward('index','index','default');                                   
        }else{
            $this->_helper->redirector('index', 'index', 'default');
        }        
    }
    public function catalogAction(){
        $modelP = new Default_Model_Product();
        $modelItem = new Default_Model_Productitem();                        
        $id = $this->_getParam('id', 0);
        if($id!=0){
            $arrVi = array('wait'=>2,'type'=>'vi','itemId'=>$id);
            $arrNo = array('wait'=>2,'type'=>'no','itemId'=>$id);
            $productVi = $modelP->getAllData($arrVi)->toArray();
            $productNo = $modelP->getAllData($arrNo)->toArray();
            foreach($productNo as $pro){
                array_push($productVi,$pro);   
            }                            
            $productA = $productVi;
            $productP = array();
            $productC = array();
            if(count($productA)>0){
                foreach($productVi as $pro){
                    ($pro['human']=='pe')?$productP[]=$pro:$productC[]=$pro;
                }
            }                                    
            $form = new Default_Form_Search();
            $this->view->form=$form;
            $this->view->productA = $productA;
            $this->view->productP = $productP;
            $this->view->productC = $productC;
            
            $proItem = $modelItem->getDataById($id)->toArray();
            $this->view->navi = $proItem['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$proItem['name'];
            $form->populate(array('catalog'=>$proItem['id']));                                                                                                                                                      
            $this->_forward('index','index','default');            
        }else{
            $this->_helper->redirector('index', 'index', 'default');
        }                            
    }
    public function deleteAction(){
        $id = $this->_getParam('id', 0);
        pr($id);
    }   
    
}